using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;

namespace IceDog.JsonDotNet.MSTest
{
    [TestClass]
    public class MyTest1
    {
        [TestMethod, Description("构建多重嵌套对象")]
        public void TestMethod1()
        {
            JObject timi = new JObject();
            timi.Add("name", "timi");
            timi.Add("likeNumber", 42);

            JObject parents = new JObject();

            JObject mother = new JObject();
            mother.Add(new JProperty("name", "alice"));
            parents.Add("mother", mother);

            JObject father = new JObject();
            father.Add(new JProperty("name", "ben"));
            parents.Add("father", father);

            timi.Add("parents", parents);

            int likeNumber = int.Parse(timi["likeNumber"].ToString());
            Assert.AreEqual(42, likeNumber);
            Assert.AreEqual("ben", timi["parents"]["father"]["name"].ToString());
            Console.WriteLine(JsonConvert.SerializeObject(timi, Formatting.Indented));

            //{
            //    "name": "timi",
            //   "likeNumber": 42,
            //   "parents": {
            //        "mother": {
            //            "name": "alice"
            //        },
            //     "father": {
            //            "name": "ben"
            //     }
            //    }
            //}

        }

        [TestMethod, Description("测试数组转换")]
        public void TestMethod2()
        {
            JObject timi = new JObject();
            timi.Add("name", "timi");

            JArray friends = new JArray();
            foreach (var num in Enumerable.Range(1, 3))
            {
                JObject friend = new JObject();
                friend.Add(new JProperty("name", $"ben {num}"));
                friends.Add(friend);
            }

            timi.Add("friends", friends);
            Assert.AreEqual(3, (timi["friends"] as JArray).Count);
            Console.WriteLine(JsonConvert.SerializeObject(timi, Formatting.Indented));

            //{
            //    "name": "timi",
            //    "friends": [
            //      {
            //        "name": "ben 1"
            //      },
            //      {
            //        "name": "ben 2"
            //      },
            //      {
            //        "name": "ben 3"
            //      }
            //    ]
            //  }
             
        }
        [TestMethod, Description("JToken类型测试")]
        public void TestMethod3()
        {
            JToken t = new JObject();
            t["name"] = "green";
            t["age"] = 32;
            var age = t.Value<int>("age");
            var name = t.Value<string>("name");
            Assert.AreEqual(32, age);
            Assert.AreEqual("green", name);
        }

        [TestMethod, Description("")]
        public void TestMethodX()
        {

        }
    }
}
